package cn.edu.swu.picture;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

//提供服务的
//包含所有的算法逻辑,比如书的涨价和降价
public class PictureService {

    private PictureRepo repo;
    public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");

    public PictureService(){
    }

    public List<Picture> getAll() throws SQLException, ClassNotFoundException {
        return this.repo.getAllPictures();
    }

    public void save(String type, String DOL, String memo, String store) throws SQLException {
        //接受东西后构造picture对象放入数据库
        Picture picture = new Picture();
        picture.setType(type);
        picture.setDOL(DOL);
        picture.setMemo(memo);
        try {
            picture.setStore(simpleDateFormat.parse(store));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        this.repo.save(picture);
    }

    public void save(Picture picture) throws SQLException {
        this.repo.save(picture);
    }

    public void deleteById(int id) throws SQLException {
        this.repo.deleteById(id);
    }

    public Picture getById(int id) throws SQLException {
        return this.repo.getById(id);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response){
    }


    public void setRepo(PictureRepo repo){
        this.repo = repo;
    }

    public void update(int id,String type, String DOL, String memo, String store, String image) throws SQLException {
        //接受东西后构造picture对象放入数据库
        Picture picture = new Picture();
        picture.setId(id);
        picture.setType(type);
        picture.setDOL(DOL);
        picture.setMemo(memo);
        try {
            picture.setStore(simpleDateFormat.parse(store));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        this.repo.update(picture);
    }

    public List<Picture> search(String key) throws SQLException {
        return this.repo.search(key);
    }
}
